//
//  CSRManager.m
//  DevSpoof
//
//  Created by huang on 2018/11/7.
//  Copyright © 2018年 wdys. All rights reserved.
//

#import "CSRManager.h"
#import <SystemConfiguration/SystemConfiguration.h>
#import "CommandLine.h"
#import "CSRContent.h"
@implementation CSRManager
+ (instancetype)shared{
    static id _instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self class] new];
    });
    return _instance;
}
- (NSString *)usename{
    SCDynamicStoreRef ref = SCDynamicStoreCreate(kCFAllocatorSystemDefault, CFSTR("user"), NULL, NULL);
    uid_t uid; gid_t gid;
    SCDynamicStoreCopyConsoleUser(ref, &uid, &gid);
    CFStringRef cfName = SCDynamicStoreCopyConsoleUser(ref, &uid, &gid);
    NSString *name = (__bridge NSString *)cfName;
    return name;
}
- (NSString *)csrFilePath{
    return [NSString stringWithFormat:@"/Users/%@/Desktop/CertificateSigningRequest.certSigningRequest",self.usename];
}
- (CSRContent *)generateNewCSRFile{
    NSString *filepath = [[NSBundle mainBundle] pathForResource:@"RandomEmail" ofType:@"py"];
    NSString *savepath = [NSString stringWithFormat:@"/Users/%@/Desktop/CertificateSigningRequest.certSigningRequest",self.usename];
    filepath = [[NSBundle mainBundle] pathForResource:@"CertificateShell" ofType:@"sh"];
    CSRContent *csr = [CSRContent generateNewCSR];
    [CommandLine command:@"/usr/bin/expect" argvs:@[filepath,savepath,csr.emailAddress]];
    return csr;
}
@end
